<template>
  <div class="myContainer">
    <!-- 头部标题 start -->  
    <el-row class="myHeadeTitle">
      <el-col :span="24" class="myPadding_20px">
        <img src="static/images/wbStep3.png" alt="" title=""/>
      </el-col>
    </el-row>
    <!-- 头部标题 end -->
    <!-- <el-row class="myPaddingTop_10px">
      <el-col>
        <el-form :inline="true" >
          <el-form-item>
            <el-input v-model="searchKW" class="myDialogSearchText" placeholder="输入关键字进行搜索"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" class="myBgGreen">搜索</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row> -->
    <el-row class="myPaddingTop_10px">
      <el-col :span="24">
        <template>
          <el-table
            v-loading="loading"
            :data="tableDataList"
            border
            @selection-change="handleSelectionChange"
            style="width: 100%">
            <el-table-column
              type="selection"
              width="100">
            </el-table-column>
            <el-table-column
              type="index"
              label="序号"
              width="100">
            </el-table-column>
            <el-table-column
              prop="name"
              label="任务名称">
            </el-table-column>
            <el-table-column
              prop="start_date"
              label="任务开始时间">
            </el-table-column>
            <el-table-column
              prop="end_date"
              label="任务结束时间">
            </el-table-column>
          </el-table>
        </template>
      </el-col>
    </el-row>
    <el-row class="myPaddingTop_10px">
      <el-col :span="24">
        <el-button type="primary" class="myBgGreen" @click="onPrevStep">上一步</el-button>
        <el-button type="primary" class="myBgGreen" @click="onNextStep" v-if="isabled">制定计划</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<style scoped>
  .myContainer{
    background: #fff;
    padding: 10px;
  }
  .myHeadeTitle{
    color: #000;
    font-weight: 500;
    font-size: 16px;
    padding-bottom: 10px;
    border-bottom: 1px solid #dbdbdb;
    margin-bottom:10px; 
  }
  .myHeadeTitle:before,.myHeadeTitle:after{
    content: " ";
    display: block;
    clear: both;
  }
  .myBgGreen{
    background: #00c2a9;
    border-color: #00c2a9;
    color: #fff;
    margin-top: 2px;
  }
  .myLineHeight_35px{
    line-height: 35px;
  }
  .myTextAlignRight{
    text-align: right;
  }
  .myBgGreen:hover{
    background: #33ceba;
  }
  .myPaddingTop_10px{
    padding-top: 10px;
  }  
  .myLeftMsg{
    line-height: 40px;
    text-align: right;
    padding-right: 10px;
  }
  .myPaddingBottom_10px{
    padding-bottom: 10px;
  }
  .myVA_M{
    vertical-align: middle;
  }
  .myPadding_20px{
    padding: 20px;
  }
  .myDialogSearchText{
    width: 360px;
  }
  .myLineHeight_45px{
    line-height: 45px;    
  }

</style>

<script>
  import { getTaskList , createMaintenancePlan} from '@/api/maintenanceplan';

  export default {
    data(){
      return{
        searchKW:"",
        tableDataList:[],
        id:"",
        cycle:"",
        index:[],
        defaultIndex:[],
        planID:"",
        loading:false,
        planname:"",
        target_objectid:"",
        isabled:true
      }
    },
    components: {
        
    },
    created(){
      document.title = '模板任务计划管理';
      this.id = this.$route.query.id;
      this.cycle = this.$route.query.cycle;
      this.planname=this.$route.query.planname;
      this.target_objectid=this.$route.query.target_objectid;
      this.onGetTaskList();
    },
    methods:{     
      handleSelectionChange:function(val){
        let vm=this;
        vm.index=[];
        vm.defaultIndex=[];
        for(let i=0;i<val.length;i++){//获取选中列的下标
          vm.index.push(val[i].idx);
        }
        for(let i=0;i<vm.tableDataList.length;i++){//获取拉取数据数组中的全部下标
          vm.defaultIndex.push(i);
        }
        vm.array_diff(vm.defaultIndex,vm.index);
        console.log("需要提交的任务项下标：",vm.defaultIndex)
      },
      onPrevStep:function(){
        this.$router.go(-1);
      },
      onNextStep:function(){
        let vm=this;
        let secondStepData=window.localStorage.getItem("secondStepData");
        if(secondStepData!=""){
          let secondStepDataJSON=JSON.parse(secondStepData);
          secondStepDataJSON.invalid_index=vm.defaultIndex;
          window.localStorage.setItem("thirdStepsData",JSON.stringify(secondStepDataJSON));//第一步数据+不执行项的下标+不执行任务下标
          console.log("制定维保计划第三步数据",secondStepDataJSON);
          vm.onCreateMaintenancePlan(secondStepDataJSON);
        }else{
          console.log("跳转失败，没有第二步的缓存数据");
        }
        
      },
      onGetTaskList(){
        let vm=this;
        vm.loading=true;
        let params={
          "template_id": vm.id,
          "plan_peroid": vm.cycle,
          "planname": vm.planname,
          "target_objectid":vm.target_objectid
        }
        getTaskList(params).then(response => {
          let res=response.data;
          if(res.length==0){
            vm.$message.error("计划周期选择有误，请前往第一步重新操作！");
            vm.isabled=false;
            vm.loading=false;
            return;
          }

          for (let i = 0; i < res.length; i++) {
            vm.tableDataList[i] = res[i];
            vm.tableDataList[i].idx = i;
          }
          console.log("拉取任务成功",vm.tableDataList)
          vm.loading=false;
        }).catch(error =>{
          console.log("拉取任务失败",error)
          vm.loading=false;
        })
      },
      onCreateMaintenancePlan(params){//创建计划
        let vm=this;
        createMaintenancePlan(params).then(response => {
          let res=response.data;
          localStorage.removeItem('back_cycleOptions');
          localStorage.removeItem('back_dutyId');
          localStorage.removeItem('back_dutyName');
          localStorage.removeItem('back_examinerId');
          localStorage.removeItem('back_examinerName');
          localStorage.removeItem('back_lastTimeValue');
          localStorage.removeItem('back_maintenanceOptions');
          localStorage.removeItem('back_maintenanceValue');
          localStorage.removeItem('back_modelOptions');
          localStorage.removeItem('back_modelValue');
          localStorage.removeItem('firstStepData');
          localStorage.removeItem('secondStepData');
          
          console.log("创建计划成功",res);
          this.$router.push({
            path:'/wbjhglfk',
            query: {type: 'add', id:res.plan_id}
          })
        }).catch(error =>{
          console.log("创建计划失败",error);
        })
      },
      array_diff(a, b) {//去除数组重复项  
        for(var i=0;i<b.length;i++){  
          for(var j=0;j<a.length;j++){  
            if(a[j]==b[i]){  
              a.splice(j,1);  
              j=j-1;  
            }  
          }  
        }   
        return a;  
      }
    }
  
  }


</script>



